package com.shelpe.services.sinbad.controller.sinbad;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.shelpe.services.sinbad.controller.dto.RankItem;
import com.shelpe.services.sinbad.controller.dto.RankingDto;
import com.shelpe.services.sinbad.controller.dto.RankingView;
import com.shelpe.services.sinbad.service.RankingService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;

@RestController
@RequestMapping(value = "/sinbadservice/v2")
@Api("v2版卡位相关的API")
public class RankingController {
	@Autowired
	private RankingService rankingService;

	@ApiOperation("创建卡位")
	@RequestMapping(path = "/users/{user_id}/rankings", method = RequestMethod.POST)
	public List<RankingDto> createRanking(@PathVariable("user_id") long userID, @RequestBody RankItem dto) {
		return this.rankingService.createRanking(userID, dto);
	}

	@ApiOperation("删除卡位")
	@RequestMapping(path = "/users/{user_id}/rankings", method = RequestMethod.DELETE)
	public List<RankingDto> deleteRanking(@PathVariable("user_id") long userID,
			@RequestParam(name = "ranking_ids") List<Long> rankingIDs) {
		Assert.isTrue(rankingIDs != null && rankingIDs.size() > 0, "无效的卡位编号");
		return this.rankingService.deleteRanking(userID, rankingIDs);
	}

	@ApiOperation("单个修改卡位")
	@RequestMapping(path = "/rankings/{ranking_id}", method = RequestMethod.PUT)
	public RankingDto updateRanking(@PathVariable("ranking_id") long rankingID, @RequestBody RankingDto dto) {
		return this.rankingService.updateRanking(rankingID, dto);
	}

	@ApiOperation("获取卡位信息")
	@RequestMapping(path = "/rankings/{ranking_id}", method = RequestMethod.GET)
	public RankingDto findRanking(@PathVariable("ranking_id") long rankingID) {
		return this.rankingService.findRanking(rankingID);
	}

	/***************************** 批量相关api ************************************/
	@ApiOperation("批量修改卡位")
	@RequestMapping(path = "/users/{user_id}/rankings", method = RequestMethod.PUT)
	public List<RankingDto> updateRankingBatch(@PathVariable("user_id") long userID, @RequestBody List<RankItem> dtos) {
		Assert.isTrue(dtos != null && dtos.size() > 0, "invalid_dtos");
		return this.rankingService.updateRanking(userID, dtos);
	}

	@ApiOperation("获取卡位列表")
	@RequestMapping(path = "/users/{user_id}/rankings", method = RequestMethod.GET)
	public List<RankingView> findRankings(@PathVariable("user_id") long userID,
			@RequestParam(name = "need_detail", required = false, defaultValue = "false") boolean needDetail) {
		return this.rankingService.findRankings(userID, needDetail);
	}

	/******************************************************************/

	@ApiOperation("获取卡位默认价格")
	@RequestMapping(path = "/adgroups/{adgroup_id}/@ranking_price", method = RequestMethod.GET)
	public RankingPriceDto findRankingPrice(@PathVariable("adgroup_id") long adgroupID) {
		return this.rankingService.findRankingPrice(adgroupID);
	}

	@Data
	public static class RankingPriceDto {
		private int price;
	}
}
